<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/13
 * Time: 23:08
 */
namespace Admin\Controller;
use Org\Yike\Tree;
use Org\Yike\Pinyin;

class DictController extends CommonController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->_mod = D('dict');
        $this->e_mod = D('estate');
    }
    public function index() {
        $tree = new Tree();
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $result = $this->_mod->get_list ();
        $array = array();
        foreach($result as $r) {
            $r['str_status'] = '<img data-tdtype="toggle" data-id="'.$r['id'].'" data-field="status" data-value="'.$r['status'].'" src="./Public/Admin/images/admin/toggle_' . ($r['status'] == 0 ? 'disabled' : 'enabled') . '.gif" />';
            $r['str_manage'] = '<a data-uri="'.U('Dict/delete',array('id'=>$r['id'])).'" data-msg="确定要删除 '.$r['title'].' 吗？" data-acttype="ajax" class="J_confirmurl" href="javascript:void(0);">删除</a> |
                                <a href="javascript:;" class="J_showdialog" data-uri="'.U('Dict/edit',array('id'=>$r['id'])).'" data-title="'.L('edit').' - '. $r['name'] .'" data-id="edit" data-width="500" data-height="360">'.L('edit').'</a>';
            $r['parentid_node'] = ($r['pid'])? ' class="child-of-node-'.$r['pid'].'"' : '';
            $r['cate_type'] = $r['type'] ? '<span class="blue">'.L('article_cate_type_'.$r['type']).'</span>' : L('article_cate_type_'.$r['type']);
            $array[] = $r;
        }
        $str  = "<tr id='node-\$id' \$parentid_node>
                <td align='center'><input type='checkbox' value='\$id' class='J_checkitem'></td>
                <td align='center'>\$id</td>
                <td align='center'>\$title</td>
				<td align='center'>\$build</td>
				<td align='center'>\$fool</td>
				<td align='center'>\$room</td>

                <td align='center'>\$str_status</td>
                <td align='center'>\$str_manage</td>
                </tr>";
        $tree->init($array);
        $list = $tree->get_tree(0, $str);
        $this->assign('list', $list);
        $big_menu = array(
            'title' => '添加数据',
            'iframe' => U('Dict/add'),
            'id' => 'add',
            'width' => '500',
            'height' => '360'
        );
        $this->assign('big_menu', $big_menu);
        $this->assign('list_table', true);
        $this->display();
    }
    public function add (){
        if (IS_POST) {
            $data = $this->_mod->create();
            if (false === $data) {
                IS_AJAX && $this->ajaxReturn(0, $this->_mod->getError());
                $this->error($this->_mod->getError());
            }
            if (method_exists($this, '_before_insert')) {
                $data = $this->_before_insert($data);
            }
            $data['add_time'] = date ('Y-m-d H:i:s');
            $data['build_name'] = empty ($data['build_name'])? $data['build']: $data['build_name'];
            $data['floor_name'] = empty ($data['floor_name'])? $data['fool']: $data['floor_name'];
            $data['room_name'] = empty ($data['room_name'])? $data['room']: $data['room_name'];
            if($this->addAll ? $this->_mod->addAll($data) :$this->_mod->add($data)){
                if( method_exists($this, '_after_insert')){
                    $id = $this->_mod->getLastInsID();
                    $this->_after_insert($id);
                }
                IS_AJAX && $this->ajaxReturn(1, L('operation_success'), '', 'add');
                $this->success(L('operation_success'));
            } else {
                IS_AJAX && $this->ajaxReturn(0, L('operation_failure'));
                $this->error(L('operation_failure'));
            }
        } else{
            //户型数据
            $house_type = M ('used_type')->where ('status=1')->select ();
            $this->assign ('house_type', $house_type);
            $response = $this->fetch();
            $this->ajaxReturn(1, '', $response);
        }

    }
}